using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using XCode;

namespace BBX.Control
{
    [DefaultProperty("Text"), ToolboxData("<{0}:DropDownList runat=server></{0}:DropDownList>")]
    public class DropDownList : System.Web.UI.WebControls.DropDownList, IPostBackDataHandler
    {
        
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public String SetFocusButtonID
        {
            get
            {
                var obj = ViewState[ClientID + "_SetFocusButtonID"];
                if (obj != null)
                {
                    return obj.ToString();
                }
                return "";
            }
            set
            {
                ViewState[ClientID + "_SetFocusButtonID"] = value;
                if (value != "")
                {
                    base.Attributes.Add("onChange", "document.getElementById('" + value + "').focus();");
                }
            }
        }

        private String _hintTitle = "";
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public String HintTitle { get { return _hintTitle; } set { _hintTitle = value; } }

        private String _hintInfo = "";
        [Bindable(true), Category("Appearance"), DefaultValue("")]
        public String HintInfo { get { return _hintInfo; } set { _hintInfo = value; } }

        private Int32 _hintLeftOffSet;
        [Bindable(true), Category("Appearance"), DefaultValue(0)]
        public Int32 HintLeftOffSet { get { return _hintLeftOffSet; } set { _hintLeftOffSet = value; } }

        private Int32 _hintTopOffSet;
        [Bindable(true), Category("Appearance"), DefaultValue(0)]
        public Int32 HintTopOffSet { get { return _hintTopOffSet; } set { _hintTopOffSet = value; } }

        private String _hintShowType = "up";
        [Bindable(true), Category("Appearance"), DefaultValue("up")]
        public String HintShowType { get { return _hintShowType; } set { _hintShowType = value; } }

        private Int32 _hintHeight = 50;
        [Bindable(true), Category("Appearance"), DefaultValue(130)]
        public Int32 HintHeight { get { return _hintHeight; } set { _hintHeight = value; } }

        public void AddTableData(DataTable dt, String textName, String valueName)
        {
            Items.Clear();
            Items.Add(new ListItem("请选择     ", "0"));
            foreach (DataRow dataRow in dt.Rows)
            {
                Items.Add(new ListItem(dataRow[textName].ToString(), dataRow[valueName].ToString()));
            }
            DataBind();
        }

        public void AddTableData(IList<IEntity> list, String textName, String valueName)
        {
            Items.Clear();
            Items.Add(new ListItem("请选择     ", "0"));
            foreach (var entity in list)
            {
                Items.Add(new ListItem(entity[textName] + "", entity[valueName] + ""));
            }
            DataBind();
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (HintInfo != "")
            {
                output.WriteBeginTag("span id=\"" + ClientID + "\"  onmouseover=\"showhintinfo(this," + HintLeftOffSet + "," + HintTopOffSet + ",'" + HintTitle + "','" + HintInfo + "','" + HintHeight + "','" + HintShowType + "');\" onmouseout=\"hidehintinfo();\">");
            }
            base.Render(output);
            if (HintInfo != "")
            {
                output.WriteEndTag("span");
            }
        }
    }
}